﻿Imports System.Web.Security
Imports System.Security.Principal
Imports System.Runtime.Serialization
Imports EagleTrackUI
Public Class MyUser


 
    Public Property User As IPrincipal
 

    Sub New(iPrincipal As IPrincipal)

        User = iPrincipal
    End Sub


    ''' <summary>
    ''' Return OrgHiarchy and Permissionlevel
    ''' </summary>
    ''' <param name="et"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OrgHiarchy(ByVal et As ETModelContainer) As IList

        Return et.MyUnits(User.Identity.Name).ToList()

    End Function


    ''' <summary>
    ''' Return OrgHiarchy and Permissionlevel
    ''' </summary>
    ''' <param name="et"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OrgHiarchyPermissions(ByVal et As ETModelContainer) As IList

        Return et.MyUnitPermissions(User.Identity.Name).ToList()

    End Function

    ''' <summary>
    ''' See if user is in the passed in role for the unit.  If user is Admin then always returns true
    ''' </summary>
    ''' <param name="Role"></param>
    ''' <param name="UnitID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsInRole(Role As String, UnitID As Integer, et As ETModelContainer) As Boolean

        Dim result As Integer = 0
        If UnitID > 0 Then
            result = (From x In et.UnitMemberships
            Join y In et.Roles On x.UnitRoleID Equals y.RoleID
             Where x.OrganizationID = UnitID And y.RoleName = Role And x.UserName = User.Identity.Name).Count
        ElseIf UnitID = ANY_UNITS Then
            result = (From x In et.UnitMemberships
             Join y In et.Roles On x.UnitRoleID Equals y.RoleID
              Where y.RoleName = Role And x.UserName = User.Identity.Name).Count
        End If
        If User.IsInRole("Admin") Then
            result = 1
        End If
        If result > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

 

End Class
